#include "STC15F2K60S2.h"
#include "ds1302.h"
#include "onewire.h"
#include "iic.h"

#define u8 unsigned char 
sbit buzzer = P0^6;
sbit relay = P0^4;
u8 ledbuff[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
u8 code LedChar[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
u8 code smg_wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

u8 Trg,Cont;
#define KEYPROUT P3
void key_read(void)
{
	u8 ReadData = KEYPROUT^0xff;
	Trg = ReadData&(ReadData^Cont);
	Cont = ReadData;
}
void Timer_Init(void)
{
	AUXR |= 0x80;		//?????1T??
	TMOD &= 0xF0;		//???????
	TL0 = 0xCD;		//??????
	TH0 = 0xD4;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????
	ET0=1;
	EA=1;
}

bit key_flag,timer_flag;
u8 shi,fen,miao,adc_save=50,adc_val,relay_zt=0,buzzer_zt=0,mode=1,xianshi=1,s6_count_1=0,s6_count_2=1;
void main(void)
{
	P2=0xa0;P0=0x00;P2=0x00;
	Timer_Init();
	set_sfm(8,30,00);
	write_adc(0x03);
	adc_save=read_24c02(0x55);
	while(1)
	{
		if((adc_save>=100)||(adc_save<0)) adc_save=50;
			
		if(s6_count_1==0)
		{
			xianshi=1;
		}
		if(s6_count_1==1)
		{
			xianshi=2;
		}
		if(xianshi==1)
		{
				ledbuff[0]=LedChar[shi/16];
				ledbuff[1]=LedChar[shi%10];
				ledbuff[2]=0xbf;
				ledbuff[3]=LedChar[fen/16];
				ledbuff[4]=LedChar[fen%16];
				ledbuff[5]=0xff;
				ledbuff[6]=LedChar[adc_val/10];
				ledbuff[7]=LedChar[adc_val%10];
		}
		if(xianshi==2)
		{
				ledbuff[0]=0xbf;
				ledbuff[1]=0xbf;
				ledbuff[2]=0xff;
				ledbuff[3]=0xff;
				ledbuff[4]=0xff;
				ledbuff[5]=0xff;
				ledbuff[6]=LedChar[adc_save/10];
				ledbuff[7]=LedChar[adc_save%10];
		}
		if(mode==1)
		{
			P2=0x80;P0=0xfe;P2=0x1f;
			if(adc_val<adc_save)
			{
				if(relay_zt!=1)
				{
					P2=0xa0;P0=0x10;P2=0x00;
					relay_zt=1;
				}
			}
			if(adc_val>=adc_save)
			{
				if(relay_zt!=0)
				{
					P2=0xa0;P0=0x00;P2=0x00;
					relay_zt=0;
				}
			}
		}
		if(mode==2)
		{
			P2=0x80;P0=0xfd;P2=0x1f;
			if(s6_count_2==1)
			{
				if(adc_val<adc_save)
				{
					if(buzzer_zt!=1)
					{
						P2=0xa0;P0=0x40;P2=0x00;
						buzzer_zt=1;
					}
				}
			}
		}
		if(key_flag)
		{
			key_flag=0;
			key_read();
			if(Trg&0x08)//s4
			{
				if(mode==2)
				{
				if(relay_zt!=0)
					{
						P2=0xa0;P0=0x00;P2=0x00;
						relay_zt=0;
					}
				}
				if(xianshi==2)
				{
					adc_save--;
				}
			}
			if(Trg&0x04)//s5
			{
				if(mode==2)
				{
				if(relay_zt!=1)
					{
						P2=0xa0;P0=0x10;P2=0x00;
						relay_zt=1;
					}
				}
				if(xianshi==2)
				{
					adc_save++;
				}
			}
			if(Trg&0x02)//s6
			{
				if(mode==1)
				{
					s6_count_1++;
					if(s6_count_1==2) 
					{
						s6_count_1=0;
						write_24c02(0x55,adc_save);
					}
				}
				if(mode==2)
				{
					s6_count_2++;
					if(s6_count_2==3) s6_count_2=1;
				if(buzzer_zt!=0)
					{
						P2=0xa0;P0=0x00;P2=0x00;
						buzzer_zt=0;
					}
				}
			}
			if(Trg&0x01)//s7
			{
				mode++;
				if(mode==3) mode=1;
			}
		}
		if(timer_flag)
		{
			timer_flag=0;
			EA=0;
			adc_val=read_adc(0x03);
			miao = Read_Ds1302(0x81);
			fen = Read_Ds1302(0x83);
			shi = Read_Ds1302(0x85);	
			EA=1;
			adc_val=adc_val*100/255;
		}
	}
	
}
void TImer0() interrupt 1 using 1
{
	static int key_count=0,smg_count=0,timer_count=0,i=0;
	key_count++;smg_count++;timer_count++;
	if(key_count==10)
	{
		key_count=0;
		key_flag=1;
	}
	if(smg_count==2)
	{
		smg_count=0;
		P2=0xc0;P0=0;P2=0;
		P2=0xe0;P0=ledbuff[i];P2=0;
		P2=0xc0;P0=smg_wei[i];P2=0;
		i++;
		if(i==8) i=0;
	}
	if(timer_count==80)
	{
		timer_count=0;
		timer_flag=1;
	}
}